<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="660px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="94px" :rules="formRules">
                <el-form-item label="任务名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入任务名称" :maxlength="100" />
                </el-form-item>
                <el-form-item label="任务说明" prop="task_description">
                    <el-input type="textarea" v-model="formData.task_description" placeholder="请输入任务说明" />
                </el-form-item>
                <el-form-item label="任务时间" prop="start_time">
                    <daterange-picker v-model:startTime="formData.start_time" v-model:endTime="formData.end_time" />
                </el-form-item>
                <el-form-item label="总目标" prop="target">
                    <el-input type="textarea" v-model="formData.target" placeholder="请输入总目标" />
                </el-form-item>
                <el-form-item label="合伙人目标" prop="partner_target">
                    <el-input type="textarea" v-model="formData.partner_target" placeholder="请输入合伙人目标" />
                </el-form-item>
                <el-form-item label="团队目标" prop="team_target">
                    <el-input type="textarea" v-model="formData.team_target" placeholder="请输入团队目标" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { taskAdd, taskDetail, taskEdit } from '@/api/task'
import Popup from '@/components/popup/index.vue'
import dayjs from "dayjs"

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑任务' : '新增任务'
})
const formData = reactive({
    id: '',
    title: '',
    task_description: '',
    target: '',
    partner_target: '',
    team_target: '',
    status: 1,
    start_time: '',
    end_time: ''
})


const formRules = {
    title: [
        {
            required: true,
            message: '请输入任务名称',
            trigger: ['blur']
        }
    ],
    task_description: [
        {
            required: true,
            message: '请输入任务说明',
            trigger: ['blur']
        }
    ],
    start_time: [
        {
            required: true,
            message: '请选择任务时间',
            trigger: ['blur']
        }
    ],
    target: [
        {
            required: true,
            message: '总目标',
            trigger: ['blur']
        }
    ],
    partner_target: [
        {
            required: true,
            message: '请输入合伙人目标',
            trigger: ['blur']
        }
    ],
    team_target: [
        {
            required: true,
            message: '请输入团队目标',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await taskEdit(formData) : await taskAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}


const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
        formData.start_time = dayjs(data.start_time).format("YYYY-MM-DD HH:mm:ss");
        formData.end_time = dayjs(data.end_time).format("YYYY-MM-DD HH:mm:ss");
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await taskDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
